//
//  DetailImageTableViewCell.m
//  BTRIP
//
//  Created by johnbk on 15/11/17.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "DetailImageTableViewCell.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "SuitableHeight.h"
#import "Blogger.h"
@implementation DetailImageTableViewCell
-(void)dealloc{
	[_button release];
	[_imgView release];
	[_detailSpecialSubModel release];
	[_labelOfDesc release];
	[super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
	self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	if (self) {
		
		[self createView];
	}
	return self;
}
- (void)createView{
	self.imgView = [[UIImageView alloc]init];
	[self.contentView addSubview:self.imgView];
	self.imgView.userInteractionEnabled = NO;
	[self.imgView release];
	
	self.button = [UIButton buttonWithType:UIButtonTypeCustom];
	[self.contentView addSubview:self.button];
	
	
	self.labelOfDesc = [[UILabel alloc]init];
	[self.contentView addSubview:self.labelOfDesc];
	[self.labelOfDesc release];
	self.labelOfDesc.font = [UIFont systemFontOfSize:12];
	//self.labelOfDesc.backgroundColor = [UIColor cyanColor];
	self.labelOfDesc.numberOfLines = 0;
	self.labelOfDesc.lineBreakMode = NSLineBreakByWordWrapping;

}
- (void)layoutSubviews{
	[super layoutSubviews];
	self.imgView.frame = CGRectMake(20, 10, self.frame.size.width - 40, self.heightOfImage);
	
	self.button.frame = CGRectMake(self.imgView.frame.origin.x, self.imgView.frame.origin.y, self.imgView.frame.size.width, self.imgView.frame.size.height);
	self.labelOfDesc.frame = CGRectMake(self.imgView.frame.origin.x, self.imgView.frame.origin.y + 10 + self.imgView.frame.size.height, self.imgView.frame.size.width, self.heightOfDesc);
	
}
-(void)setDetailSpecialSubModel:(DetailSpecialSubModel *)detailSpecialSubModel{
	if (_detailSpecialSubModel != detailSpecialSubModel) {
		[_detailSpecialSubModel release];
		_detailSpecialSubModel = [detailSpecialSubModel retain];
		[self layoutSubviews];
		self.heightOfImage = self.imgView.frame.size.width * (detailSpecialSubModel.image_height.integerValue) / (detailSpecialSubModel.image_width.integerValue);
		
		self.heightOfDesc = [SuitableHeight suitableHeight:detailSpecialSubModel.desp andWidth:self.labelOfDesc.frame.size.width andFont:12];
		NSArray *array = [detailSpecialSubModel.desp componentsSeparatedByString:@"@"];
		NSMutableString *handledDesc = [NSMutableString string];
		if ([array[0] hasPrefix:@"#"]) {
			for (int i = 1; i < array.count; i++) {
				for (Blogger *blogger in detailSpecialSubModel.arrayOfBlogger) {
					if ([array[i] containsString:blogger.number]) {
						NSMutableString *mutableString = [NSMutableString stringWithString:array[i]];
						NSRange range = [array[i] rangeOfString:blogger.number];
						
						[mutableString replaceCharactersInRange:range withString:blogger.name];
						[handledDesc appendString:[NSMutableString stringWithFormat:@"@%@", mutableString]];
					}
				}
			
			}
			
			NSString *stringOfDesc = [NSString stringWithFormat:@"%@%@", array[0], handledDesc];
			NSArray *arrayTwo = [stringOfDesc componentsSeparatedByString:@"@"];
			NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:stringOfDesc];
			
			for (int i = 1; i < arrayTwo.count; i++) {
				for (Blogger *blogger in detailSpecialSubModel.arrayOfBlogger) {
					if ([arrayTwo[i] containsString:blogger.name]) {
						[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSRangeFromString(blogger.name)];
					}
				}
				
				
			}
			
			
			[self.labelOfDesc setAttributedText:attributedString];
			
		}else if ([array[0] hasPrefix:@"@"]){
			for (int i = 0; i < array.count; i++) {
				for (Blogger *blogger in detailSpecialSubModel.arrayOfBlogger) {
					if ([array[i] containsString:blogger.number]) {
						NSMutableString *mutableString = [NSMutableString stringWithString:array[i]];
						NSRange range = [array[i] rangeOfString:blogger.number];
						
						[mutableString replaceCharactersInRange:range withString:blogger.name];
						[handledDesc appendString:[NSMutableString stringWithFormat:@"@%@", mutableString]];
					}
				}
		}
		
		NSString *stringOfDesc = [NSString stringWithFormat:@"%@%@", array[0], handledDesc];
		NSArray *arrayTwo = [stringOfDesc componentsSeparatedByString:@"@"];
		NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:stringOfDesc];
		
		for (int i = 1; i < arrayTwo.count; i++) {
			for (Blogger *blogger in detailSpecialSubModel.arrayOfBlogger) {
				if ([arrayTwo[i] containsString:blogger.name]) {
					[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSRangeFromString(blogger.name)];
				}
			}
		}
		}else{
			self.labelOfDesc.text = detailSpecialSubModel.desp;
		}
		
		[self.imgView sd_setImageWithURL:[NSURL URLWithString:detailSpecialSubModel.image_url] placeholderImage:[UIImage imageNamed:@"background"]];
				[self layoutSubviews];
		
	}
}
+ (DetailImageTableViewCell *)tableView:(UITableView *)tableView{
	
	static NSString *cellIdentifier = @"DetailImageTableViewCellIdentifier";
	
	DetailImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
	
	if (!cell) {
		cell = [[DetailImageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
		
	}
	return cell;
	
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
